import http from 'http';
const port = 7890
const texts = [
  "轻轻的我走了，",
  "正如我轻轻的来；",
  "我轻轻的招手，",
  "作别西天的云彩。",
];

const server = http.createServer((req, res) => {
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.setHeader("Access-Control-Allow-Headers", "*");
  res.setHeader("Content-Type", "text/event-stream; charset=utf-8");
  res.setHeader("Cache-Control", "no-cache");
  res.setHeader("Connection", "keep-alive");
//   res.setHeader("Transfer-Encoding", "chunked");

  let index = 0;
  res.write(texts[index]);
  const timer = setInterval(() => {
    index++;
    res.write(texts[index]);
    if (index === texts.length - 1) {
      clearInterval(timer);
      res.end();
    }
  }, 1000);
});

server.listen(port, () => {
  console.log(`启动: http://localhost:${port}`);
});